import Game from "./game";
import { isValid, isValidAndNoEmptyString } from '../Utils';
import { Vector2 } from './gameClass';
import Expression from "./expression";

export abstract class UVariable<T>
{
    game!: Game;
    /** 如果是null则不包含该内容 */
    srcValue!: string;
    // alias!: string[];
    /** 是否存在该变量 */
    defaultValue?: T;
    lastValue!: T;
    protected _value!: T;

    isExpression: boolean = false;
    expression!: Expression;
    /** 表达式执行结果 */
    expressionResult: any;

    get value() {
        this.lastValue = this._value;
        return this._value;
    }

    constructor(defaultValue?: T) {
        // this.alias = alias;
        this.defaultValue = defaultValue;
    }
    /**
     * 初始化,检验，解析字符串, 发现onNeedChange和其他功能一致，现在init默认调用onNeedChange
     */
    init() {  
        if (isValidAndNoEmptyString(this.srcValue)) {
            this.isExpression = Expression.isIncludeExpression(this.srcValue);
            if(this.isExpression) {
                this.expression = Expression.parse(this.srcValue, this.game.customVariables);
                this.execExpression();
            }
        }
        
        this.dataConvert();
    }

    dataConvert() {
        if(this.isExpression) {
            this.onDataConvert(this.expressionResult);
        }
        else {
            this.onDataConvert(this.srcValue);
        }
    }
    /**数据转换 */
    protected abstract onDataConvert(value: any) : any;

    /**执行表达式 */
    execExpression() {
        this.expressionResult = this.expression.eval(this.game.customVariables);
    }

    /** 在调用value前需要调用该方法 */
    needChange(forceReturnTrue: boolean = false) {
        if(this.isExpression) {
            this.execExpression();
            this.dataConvert();
        }
        return forceReturnTrue? true : this.lastValue !== this._value;
    }

    srcValueIsValid() {
        return isValid(this.srcValue);
    }

    calcValue() : T {
        this.needChange();
        return this.value;
    }
}


export class UNumber extends UVariable<number> { 
    protected onDataConvert(value: any) {
        if(this.isExpression) {
            if (typeof this.expressionResult != "number") {
                this.game.printError("警告错误");
            }
        }
        if(!isValidAndNoEmptyString(value)) {
            this._value = this.defaultValue!;
        }
        else {
            this._value = parseFloat(value);
        }
    }

}

export class UString extends UVariable<string> { 
    protected onDataConvert(value: any){
        if(this.isExpression) {
            if (typeof this.expressionResult != "string") {
                this.game.printError("警告错误");
            }
        }
        if(isValid(this.srcValue)) {
            this._value = value;
        }
        else {
            this._value = this.defaultValue!;
        }
        if(typeof this._value == "string") {
            this._value = this._value.replaceAll("\\n", "<br>");
        }
    }

}

export class UStringArray extends UVariable<string[]> {
    protected onDataConvert(value: any){
        if(this.isExpression) {
            if (typeof this.expressionResult != "number") {
                this.game.printError("警告错误");
            }
        }
        if(typeof value == "string" && value != "") {
            this._value = value.split(",")
        }
        else {
            this._value = this.defaultValue!;
        }
    }
}


export class UBoolean extends UVariable<boolean> { 
    protected onDataConvert(value: any) {
        if(this.isExpression) {
            if (typeof this.expressionResult != "boolean") {
                this.game.printError("警告错误");
            }
        }
        if(!isValid(value) && isValid(this.defaultValue)) {
            this._value = this.defaultValue!;
        }
        else {
            this._value = value != "false" && value !== false;
        }
    }
}

// export class UVector2 extends UVariable<Vector2> {
//     onNeedChange() {

//         return false;
//     }
// }

export class UFontSize extends UVariable<string> {
    protected onDataConvert(value: any) {
        if(this.isExpression) {
            if (typeof this.expressionResult != "number") {
                this.game.printError("警告错误");
            }
        }

        if(isValidAndNoEmptyString(value)) {
            value = parseFloat(value);
            this._value = this.game.calcLength(value);
        }
        else if(this.defaultValue == null) {
            this._value = this.game.defaultFontSize()
        }
        else {
            this._value = this.defaultValue!;
        }
    }
}

export class ULength extends UVariable<string> {
    protected onDataConvert(value: any) {
        if(this.isExpression) {
            if (typeof this.expressionResult != "number") {
                this.game.printError("警告错误");
            }
        }
        if(value) {
            this._value = this.game.calcLength(value); //(parseFloat(value) / this.game.actualFontSize).toFixed(3) + "rem";
        }
        else {
            this._value = "0rem";
        }
    }

}

export class UAny extends UVariable<any> {
    protected onDataConvert(value: string) { 
        this._value = value
    };
}